package com.cardgame.model.level;

import com.cardgame.model.card.TotalWeaponCard;
import com.cardgame.model.role.Role;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

// 主角战斗状态封装类
@Getter
public class LevelMainRole {
    private final Role role;
    private int currentHP;
    private int currentMP;
    private TotalWeaponCard weapon;
    @Getter
    @Setter
    private int maxMP;

    public LevelMainRole(Role role) {
        this.role = role;
        this.maxMP = role.getMp();
        this.currentHP = role.getHp();
        this.currentMP = role.getMp();
        this.weapon = null;
    }

    public int getMaxHP() {
        return role.getHp() + (weapon!= null? weapon.getAddHPMax() : 0);
    }

    public void setWeapon(TotalWeaponCard weapon) {
        this.weapon = weapon;
        setCurrentHP(currentHP + weapon.getAddHPMax());
    }

    public void setCurrentHP(int hp) {
        this.currentHP = Math.min(hp, getMaxHP());
    }

    public void setCurrentMP(int mp) {
        this.currentMP = Math.min(mp, getMaxMP());
    }

    public boolean useMP(int amount) {
        if (currentMP < amount)
            return true;
        currentMP -= amount;
        return false;
    }

    public void resetMP() {
        currentMP = getMaxMP();
    }

    @Override
    public String toString() {
        return "LevelMainRole{" +
                "role=" + role +
                ", currentHP=" + currentHP +
                ", currentMP=" + currentMP +
                ", maxHP=" + getMaxHP() +
                ", maxMP=" + getMaxMP() +
                ", weapon=" + weapon +
                '}';
    }

    public Map<String, Object> toMap() {
        Map<String, Object> result = new HashMap<>(role.toMap());
        result.put("maxHp", getMaxHP());
        result.put("currentHp", currentHP);
        result.put("maxMp", getMaxMP());
        result.put("currentMp", currentMP);
        if (weapon!= null)
            result.put("weapon", weapon.toMap());
        return result;
    }

}
